<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\ApplyWith as UM;
use app\admin\model\Role;
use app\admin\validate\ApplyWithValidate;

class ApplyWith extends Common
{
    private $noCheckAction = [];//不需要验证的方法
    
    public function initialize(){
        parent::initialize();
        $this->doCheckAuth($this->noCheckAction);
    }
    /**
    * 列表
    */
    public function index()
    {
        
        $model = new UM();
        $request = $this->request->get();
        $search_where = $model->_where($request);
        $where = $search_where['where'];
        $search = $search_where['search'];
        $data = $model->_list($where);
        $count = $model->_count($where);
        
        $types = UM::ApplyWith_TYPE;
        
        $this->assign([
            'search' => $search,
            'list' => $data['list'],
            'page' => $data['page'],
            'count' => $count,
            'types'=>$types
        ]);
        
        return $this->fetch();
    }

    
    /**
    * 删除
    */
    public function del(){
        if($this->request->isAjax()){
            $id = (int)$this->request->post('id');
            
            $validate = new ApplyWithValidate();
            if(!$validate->scene($validate::SCENE_REMOVE)->check(['id'=>$id])){
                die(outputError($validate->getError()));
            }
            
            $model = new UM();
            $ret = $model->_remove($id);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    /**
    * 批量删除
    */
    public function delAll(){
        if($this->request->isAjax()){
            $ids = (string)$this->request->post('ids');
            $model = new UM();
            $ret = $model->_removeAll($ids);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    
   
    
    /**
    * 同意提现
    */
    public function agree(){
        $request = $this->request->post();
        $validate = new ApplyWithValidate();
        if(!$validate->scene($validate::SCENE_AGRRE)->check(['id'=>$request['id']])){
            die(outputError($validate->getError()));
        }
        $model = new UM();
        $id = (int)$request['id'];
        $admin = $this->admin;
        
        $ret = $model->_agree($id, $admin['id']);
        if(!$ret){
            die(outputError($model->getError()));
        }
        
        die(outputSuccess());
    }
    
    /**
    * 拒绝提现
    */
    public function refuse(){
        $request = $this->request->post();
        $validate = new ApplyWithValidate();
        if(!$validate->scene($validate::SCENE_REFUSE)->check(['id'=>$request['id']])){
            die(outputError($validate->getError()));
        }
        $model = new UM();
        $id = (int)$request['id'];
        $admin = $this->admin;
        $ret = $model->_refuse($id, $admin['id']);
        if(!$ret){
            die(outputError($model->getError()));
        }
        
        die(outputSuccess());
    }
}
